package se.boliang.ccstore;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Service {
	private static boolean isServiceIdle = true; 
	private static List<Customer> customerList = new ArrayList<Customer>(); 
	private static long endTime; 
	
	public static void processService(Customer customer) {
		isServiceIdle = false; 
		Random serviceRandom = new Random();
		try {
			Thread.sleep((long) (serviceRandom.nextFloat() * 1000));
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 	
		endTime = System.currentTimeMillis(); 
		
		System.out.println("Customer #" + customer.getI() + " Service Finished!");
		System.out.println("Service Time : " + serviceRandom.nextFloat() * 10); 
		System.out.println("Total Time : " + (endTime - customer.getStartTime()) / 1000.00); 
		findNextCustomer(); 
	}

	public static boolean isServiceIdle() {
		return isServiceIdle;
	}
	
	public static void addCustomer(Customer customer) {
		customerList.add(customer); 
//		System.out.println(customer); 
	}
	
	public static void findNextCustomer() {
		if (customerList.isEmpty() == false) {
			Customer customer = customerList.get(0);
			customerList.remove(0); 
			processService(customer); 
		} else {
			isServiceIdle = true; 
		}
	}
}
